package member

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	auth_user "go-papers/common/auth-user"
	"go-papers/models/protocols"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
	"go-papers/tools/app/msg"
)

func CompareContrast(c *gin.Context) {
	var data models.MemberContrast
	var err error

	data.OpenId = c.Request.FormValue("openId")
	data.ContrastStatus = "1"
	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)

	if ok, msgs := auth_user.VerifyUid(c, data.MemberId, data.ThirdUserId); ok {
		app.CommonResp(c, 126, msgs, nil)
		return
	}

	result, count, err := data.GetComparePage()
	if err != nil {
		tools.HasError(err, "异常:"+err.Error(), -1)
	}

	// @TODO 过滤相同字段
	if len(result) >= 2 {

	}

	app.PageOK(c, result, count, 1, count, "ok")
}

func GetMemberContrastList(c *gin.Context) {
	var data models.MemberContrast
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	if ContrastId := c.Request.FormValue("contrastId"); ContrastId != "" {
		data.ContrastId = tools.StrToInt(err, ContrastId)
	}
	if memberId := c.Request.FormValue("memberId"); memberId != "" {
		data.MemberId = tools.StrToInt(err, memberId)
	}
	if courseId := c.Request.FormValue("courseId"); courseId != "" {
		data.CourseId = tools.StrToInt(err, courseId)
	}

	data.OpenId = c.Request.FormValue("openId")
	data.ContrastStatus = c.Request.FormValue("contrastStatus")
	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)
	if ok, msgs := auth_user.VerifyUid(c, data.MemberId, data.ThirdUserId); ok {
		app.CommonResp(c, 126, msgs, nil)
		return
	}

	data.DataScope = tools.GetUserIdStr(c)
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetMemberContrast(c *gin.Context) {
	var data models.MemberContrast
	data.ContrastId, _ = tools.StringToInt(c.Param("contrastId"))
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func InsertMemberContrast(c *gin.Context) {
	var data models.MemberContrast
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)

	if ok, msgs := auth_user.VerifyUid(c, data.MemberId, data.ThirdUserId); ok {
		app.CommonResp(c, 126, msgs, nil)
		return
	}

	sr, _ := data.GetV2()
	if sr.ContrastId != 0 {
		if sr.ContrastStatus != "1" {
			sr.ContrastStatus = "1"
			_, _ = sr.Update(sr.ContrastId)
		}

		app.CommonResp(c, 0, "记录已经存在", sr)
		return
	}
	data.ContrastStatus = "1"
	result, err := data.Create()
	if err != nil {
		tools.HasError(err, "失败："+err.Error(), -1)
	}

	app.OK(c, result, "成功")
}

func UpdateMemberContrast(c *gin.Context) {
	var pp protocols.ContrastReq
	err := c.BindWith(&pp, binding.JSON)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	if len(pp.Ids) < 0 {
		app.ApiResp(c, -1, "数据解析失败 ids 为空", nil)
		return
	}

	var data models.MemberContrast

	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, pp.OpenId)
	if ok, msgs := auth_user.VerifyUid(c, data.MemberId, data.ThirdUserId); ok {
		app.CommonResp(c, 126, msgs, nil)
		return
	}

	result, err := data.BatchUpdateStatus(pp.Ids, pp.ContrastStatus)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "成功")
}

func DeleteMemberContrast(c *gin.Context) {
	var data models.MemberContrast

	data.OpenId = c.Request.FormValue("openId")
	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)
	contrastIds := c.Request.FormValue("contrastId")
	IDS := tools.IdsStrToIdsIntGroupV2(contrastIds)
	_, err := data.BatchDelete(IDS)
	if err != nil {
		app.ApiResp(c, -1, "数据删除失败："+err.Error(), nil)
		return
	}

	app.OK(c, nil, msg.DeletedSuccess)
}
